﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATCCL;
using ATEC.GeometryNET;
using System.Windows.Forms;
using DefinePropsInput;
using ATEC.CCLObj.Entity;


namespace PropsItemExample
{
    public class AddPropToObjInAttachExample
    {
        private static string attachSceneName = "参照场景挂载属性示例";

        private static ATEC.ATDataCoreNET.ATObjects.ATObject m_obj;
        //获取场景
        private static ATScene GetATScene(ATProject project, string scenceName)
        {
            SceneID sceneID = project.DefaultSceneId;
            if (!scenceName.Equals("default"))
                sceneID = project.FindSceneIdByName(scenceName);

            StatusInt status = StatusInt.Error;
            ATScene scene = project.LoadRootSceneById(ref status, sceneID);
            return scene;
        }
        //获取参考场景
        public static ATAttachment GetAttachment()
        {
            ATAttachment resAttachment = null;
            ATProject activeProject = ATEC.ATCore.Session.Entity.GetActiveATProject();
            ATScene mainScene = GetATScene(activeProject, "三维模型空间");
            if (mainScene == null || !mainScene.IsValid())
                return resAttachment;

            foreach (ATAttachment attachment in mainScene.GetATAttachments())
            {
                if (attachment.AttachSceneName.Equals(attachSceneName) && attachment != null)
                    resAttachment = attachment;
            }

            return resAttachment;
        }
        //创建一个场景并参照到三维模型空间中
        public static void CreateSceneAndAttachToScene3D()
        {
            //创建名为“参照场景挂载属性示例”的场景
            ATProject activeProject = ATEC.ATCore.Session.Entity.GetActiveATProject();

            ATScenesStatus status;
            ATScene attachScene = activeProject.CreateNewScene(out status, attachSceneName, ATScenesType.Normal, true, null);
            if (status != ATScenesStatus.Success || !attachScene.IsValid())
                attachScene = GetATScene(activeProject, attachSceneName);
            //创建一个几何对象添加到场景中
            XPoint3d pt1 = XPoint3d.FromXYZ(100, 1000, 1000);
            XPoint3d pt2 = XPoint3d.FromXYZ(100, 1000, -1000);
            XPoint3d pt3 = XPoint3d.FromXYZ(100, -1000, -1000);
            XPoint3d pt4 = XPoint3d.FromXYZ(100, -1000, 1000);
            List<XPoint3d> pts = new List<ATEC.GeometryNET.XPoint3d> { pt1, pt2, pt3, pt4, pt1 };
            CurveSet tempCurveSet = CurveSet.CreateLinear(pts, CurveSet.BoundaryType.Outer, true);
            m_obj = ATEC.ATDataCoreNET.ATObjects.GeometryToATObject.ToObject(attachScene.GetATScene(), tempCurveSet, null);
            m_obj.AddToScene();
            //获取三维模型空间          
            ATScene scene3D = GetATScene(activeProject, "三维模型空间");
            //参照
            if (scene3D == null)
                return;

            ATDocMoniker docMoniker = activeProject.GetDocument().GetMoniker();
            ATAttachment attachment = scene3D.CreateATAttachment(docMoniker, attachSceneName);
            if (attachment == null)
                return;

            attachment.NestDepth = 99;
            attachment.WriteToScene(false);
        }

        //对参照的场景挂接属性
        public static void AddPropToAttachScene()
        {
            ATAttachment attachment = GetAttachment();
            string libraryName = "PropLib";
            ATProject file = ATEC.ATCore.Session.Entity.GetActiveATProject();


            PropsItemLibrary itemTypeLibrary = PropsItemLibrary.FindByName(libraryName, file);
            if (itemTypeLibrary == null)
            {
                // 没有则创建Library
                itemTypeLibrary = PropsItemLibrary.Create(libraryName, file);
            }

            // 获取输入的属性类型名称
            string itemTypeName = "propType";

            // 获取属性类型
            PropsItem itemType = itemTypeLibrary.GetPropsItemByName(itemTypeName);
            if (itemType == null)
            {
                // 没有则创建
                itemType = itemTypeLibrary.AddPropsItem(itemTypeName);
            }

            // 获取输入的属性项名称
            string propName = "propName";
            // 查找属性
            CustomProperty testProperty = itemType.GetPropertyByName(propName);
            if (testProperty != null)
            {
                itemType.RemoveProperty(testProperty);
                itemTypeLibrary.Write();
            }
            // 增加属性
            testProperty = itemType.AddProperty(propName, true);
            testProperty.Type = CustomProperty.TypeKind.String;
            testProperty.DefaultValue = "propValue";

            // 写入属性到Library
            itemTypeLibrary.Write();

            CustomPropsItemHost host = new CustomPropsItemHost(m_obj, true);
            IATCCLEntity entity = host.ApplyCustomPropsItem(itemType, true);
            string acturPropName = "propName";
            ATEC.CCLObj.CCFWork.CCLNameValidation.EncodeToValidName(ref acturPropName);

            entity.SetString(acturPropName, "testValue");
            entity.ScheduleChanges(m_obj);
            m_obj.ReplaceInScene(m_obj);
            file.ProcessChanges(ATSaveReason.ApplicationInitiated);
            SSDPNET.AtRefProjectFuncs.RefFileReload(attachment, true, true);
        }
    }
}
